package main

import (
	"html/template"
	"im_chat_app/controller"
	"log"
	"net/http"
)

func RegisterView() {
	tpl, err := template.ParseGlob("view/**/*")
	// 如果报错就不要继续执行了
	if err != nil {
		// 打印并直接退出
		log.Fatal(err.Error())
	}
	for _, v := range tpl.Templates() {
		tplName := v.Name()
		http.HandleFunc(tplName, func(w http.ResponseWriter, r *http.Request) {
			tpl.ExecuteTemplate(w, tplName, nil)
		})
	}

}

func main() {

	// 绑定请求和处理函数
	http.HandleFunc("/user/login", controller.UserLogin)
	http.HandleFunc("/user/register", controller.UserRegister)

	http.HandleFunc("/contact/loadcommunity", controller.LoadCommunity)
	http.HandleFunc("/contact/loadfriend", controller.LoadFriend)
	http.HandleFunc("/contact/joincommunity", controller.JoinCommunity)
	http.HandleFunc("/contact/createcommunity", controller.CreateCommunity)
	//http.HandleFunc("/contact/addfriend", controller.Addfriend)
	http.HandleFunc("/contact/addfriend", controller.Addfriend)

	http.HandleFunc("/chat", controller.Chat)

	http.HandleFunc("/attach/upload", controller.Upload)

	http.Handle("/asset/", http.FileServer(http.Dir(".")))
	http.Handle("/mnt/", http.FileServer(http.Dir(".")))

	RegisterView()
	// 启动web服务器
	http.ListenAndServe(":8080", nil)
}

// 数据库相关
//  go get github.com/go-xorm/xorm
// go get github.com/go-sql-driver/mysql
